package com.spring.housing.util;

import com.spring.housing.returnType.CommenResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;
/*
@autor lzm 2022-02-28
*@tableName查询的表的名字
* @queryArray 要查询的内容
* @Limitparams 查询的条件
* @params 查询的条件参数
 */

@Slf4j
@Component
public class QueryTemplateForJdbc {
    @Autowired
    JdbcTemplate jdbcTemplate;
    public  CommenResult queryForList(String tableName,String[] queryArray,String[] Limitparams,String...params){
        String sql="select  ";
        for (int i = 0; i <queryArray.length ; i++) {
            if(i==queryArray.length-1){
                sql += queryArray[i];
            }
            else {
                sql += queryArray[i]+",";
            }
        }

        sql +=" from  "+tableName+" where  1=1 ";
        for (int i = 0; i <Limitparams.length ; i++) {
                sql +=("and "+Limitparams[i]+"= ? ");
        }
        log.info("查询语句为:"+sql);
        List<Map<String, Object>> list = jdbcTemplate.queryForList(sql, params);
        CommenResult res=     new CommenResult<List<Map<String, Object>>>(200,"查询"+tableName+"表成功!",list);
        return  res;
    }
}
